package io.renren.modules.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.shop.entity.ProductDetailEntity;
import io.renren.modules.shop.entity.ProductPictureEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.shop.entity.ProductEntity;
import io.renren.modules.shop.service.ProductService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author Yangqianyong
 * @email sunlightcs@gmail.com
 * @date 2021-11-17 15:35:51
 */
@RestController
@RequestMapping("shop/product")
public class ProductController {
    @Autowired
    private ProductService productService;


    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("shop:product:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{productId}")
//    @RequiresPermissions("shop:product:info")
    public R info(@PathVariable("productId") Long productId){
		ProductEntity product = productService.getById(productId);

        return R.ok().put("product", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("shop:product:save")
    public R save(@RequestBody ProductEntity product){
		productService.save(product);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("shop:product:update")
    public R update(@RequestBody ProductEntity product){
		productService.updateById(product);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("shop:product:delete")
    public R delete(@RequestBody Long[] productIds){
		productService.removeByIds(Arrays.asList(productIds));

        return R.ok();
    }

    //1.getDetails：根据商品id获取商品详细信息
    @RequestMapping("/detail")
    public R getDetails(@RequestParam("id") Long id){
        ProductDetailEntity productDetailEntity = productService.getDetailById(id);
        return R.ok().put("list",productDetailEntity);
    }

    //2.getDetailsPicture：根据商品id获取商品图片
    @RequestMapping("/pro_pictures")
    public R getDetailsPicture(@RequestParam("id") Long id){

        List<ProductPictureEntity> pictureEntityList = productService.getProductPicture(id);
        return R.ok().put("list",pictureEntityList);
    }
//        3.getProductBySearch：通过搜索条件查询商品
    @RequestMapping("/search")
    public R getProductBySearch(@RequestParam("search") String search){

        List<ProductDetailEntity> productDetailEntity = productService
                .getProductBySearch(search);
        return R.ok().put("list",productDetailEntity);
    }

    //        3.getProductBySearch：通过搜索条件查询商品
    @RequestMapping("/searchOne")
    public R getProductBySearchOne(@RequestParam("search") String search,@RequestParam Integer rack){

        List<ProductDetailEntity> productDetailEntity = productService
                .getProductBySearch(search,rack);
        return R.ok().put("list",productDetailEntity);
    }

    //        4.getProductByCategory：根据商品种类查询商品
    @RequestMapping("/ca_search")
    public R getProductByCategory(@RequestParam("categoryID") Integer categoryID){
        List<ProductDetailEntity> productDetailEntity = productService
                .getProductByCategory(categoryID);
        return R.ok().put("list",productDetailEntity);
    }

    @RequestMapping("/ca_searchOne")
    public R getProductByCategoryOne(@RequestParam("categoryID") Integer categoryID,@RequestParam Integer rack){
        List<ProductDetailEntity> productDetailEntity = productService
                .getProductByCategory(categoryID,rack);
        return R.ok().put("list",productDetailEntity);
    }

//    @RequestMapping("/ca_search1")
//    public R getProductByCategory1(@RequestBody ProductDetailEntity productDetail){
//        System.out.println(productDetail.toString());
//        List<ProductDetailEntity> productDetailEntity = productService
//                .getProductByCategory(productDetail.getCategoryId());
//        return R.ok().put("list",productDetailEntity);
//    }
//
//    @RequestMapping("/ca_search2")
//    public R getProductByCategory2(@RequestParam Map<String, Object> params,@RequestParam("categoryID") Integer categoryID){
//
//        PageUtils page = productService.queryPage(params,categoryID);
//
//        return R.ok().put("page",page);
//    }
}
